package com.bw.user.mvvm.viewmodel;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.databinding.ObservableField;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.UserDaoEntity;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.SPUtils;
import com.bw.user.entity.LoginEntity;
import com.bw.user.mvvm.model.LoginModel;
import com.bw.user.mvvm.view.activity.FindPwdActivity;
import com.bw.user.mvvm.view.activity.RegiserActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.concurrent.Executors;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/19 11:09
 */
public class LoginViewModel extends BaseViewModel<LoginModel> {

    public ObservableField<String> username = new ObservableField<>();
    public ObservableField<String> pwd = new ObservableField<>();


    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity.getMessage().equals("登录成功")){
            showMsg("登录成功");
            EventBus.getDefault().postSticky(username.get());
            LoginEntity loginEntity = (LoginEntity) baseEntity;
            //存入数据库
            UserEntity userEntity = new UserEntity();
            userEntity.setId(1l);
            userEntity.setUser_name(username.get());
            userEntity.setUser_pwd(pwd.get());
            userEntity.setUser_id(loginEntity.getData().getId());
            userEntity.setFlag(true);
            DaoSessionUtils.getInstance().getSession().insertOrReplace(userEntity);

            finishPage();
        }else {
            showMsg("登录失败");
        }
    }

    //登录按钮点击事件
    public void loginClick(){
        if(TextUtils.isEmpty(username.get())||TextUtils.isEmpty(pwd.get())){
            showMsg("用户名或密码为空");
        }else {

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("mobile",username.get());
            hashMap.put("pushId","1");
            hashMap.put("pwd",pwd.get());
            m.requestLogin(hashMap)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);

        }

    }

    //注册按钮点击事件
    public void regiserClick(){
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, RegiserActivity.class);
        getUiChanage().getStartPageObservable().setValue(map);
        finishPage();


    }


    //返回
    public void back(){
        finishPage();
    }

    //找回密码
    public void findpwd(){
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, FindPwdActivity.class);
        startPage(map);
    }
}
